var availableTags = [];//数据源
$(function(){

	// 监测到输入框变化事件
	/*$("#inp").bind('input porpertychange',function(){
		searchWeather($("#inp").val())
	});*/

	$('#search').click(function(){
		searchWeather($("#inp").val())
	})

	loadingKey()

	$( "#inp" ).autocomplete({//调用补全功能
      source: availableTags
    });
});

function searchWeather (address) {
	// body...	
	$.ajax({
		cache: false,
		type: 'get',
		url: urls + '?city=' + address,
		dataType:'json',//数据类型
		// dataType:"jsonp",    //跨域json请求一定是jsonp
		// jsonp: "callback",    //跨域请求的参数名，默认是callback
		async : false,
		timeout:2000,//超时
		success: function (res) {
			if (res && res.status === 200) {
				$("#look").html(res.cityInfo.city)
			}else{
				$("#look").html("Query city does not exist")
			};
		},
		error: function (msg) {
			console.log(msg)
		}
    });
}

function loadingKey () {
	ajaxGet(searchKey,function(result){
		if (result && result.success) {
			availableTags = result.data
		};
	})
	// $.ajax({
	// 	cache: false,
	// 	type: 'get',
	// 	url: searchKey,
	// 	dataType:'json',
	// 	async : false,
	// 	timeout:2000,//超时
	// 	success: function (res) {
	// 		availableTags = res;
	// 	},
	// 	error: function (XMLHttpRequest, textStatus, errorThrown) {
		
	// 	}
 //    });
}


function ajaxGet(url,callback){
	$.ajax({
		cache: false,
		type: 'get',
		url: searchKey,
		dataType:'json',
		async : false,
		timeout:2000,//超时
		success: function (res) {
			callback(res)
		},
		error: function (msg) {
			callback({success: false, msg: '错误！'})
		}
    });
}